home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / SeqPups / appsrc / autoseq.src / CSequence.C < prev    next >
Text File  |  1996-07-05  |  4KB  |  116 lines

  1. //    ============================================================================
  2. //    CSequence.C                                                        80 columns
  3. //    Reece Hart    (reece@ibc.wustl.edu)                                tab=4 spaces
  4. //    Washington University School of Medicine, St. Louis, Missouri
  5. //    This source is hereby released to the public domain.  Bug reports, code
  6. //    contributions, and suggestions are appreciated (to email address above).
  7. //    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -    -
  8. //    Please see CSequence.H for a description of the CSequence class.
  9. //    ========================================|===================================
  10.  
  11. #include    "CSequence.H"
  12.  
  13. template<class T>
  14. vrsn CSequence<T>::version = "94.05.03";
  15.  
  16. //    ============================================================================
  17. //    Prepend
  18. //    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
  19. //    Prepends an element of type T to a list.
  20. //    ========================================|===================================
  21. template<class T>
  22. bool
  23. CSequence<T>::Prepend(T item)
  24.     {
  25.     SeqNode<T>*    sn = new SeqNode<T>;
  26.     if (NULL == sn)
  27.         return TRUE;                        // couldn't allocate new SeqNode
  28.     sn->next = first;                        // new node points to old first
  29.     if (NULL != first)                        // old first node points
  30.         first->prev = sn;                    //   to new node
  31.     first = sn;                                // update first node pointer
  32.     if (NULL == last)                        // item added to empty list
  33.         last = sn;
  34.     sn->data = item;                        // set the data
  35.     size++;                                    // increment our size counter
  36.     return FALSE;                            // return no error
  37.     }
  38.  
  39. //    ============================================================================
  40. //    Append
  41. //    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
  42. //    Appends an element of type T to a list.
  43. //    ========================================|===================================
  44. template<class T>
  45. bool
  46. CSequence<T>::Append(T item)
  47.     {
  48.     SeqNode<T>*    sn = new SeqNode<T>;
  49.     if (NULL == sn)
  50.         return TRUE;                        // couldn't allocate new SeqNode
  51.     sn->prev = last;                        // new node points to old last
  52.     if (NULL != last)                        // old last node points
  53.         last->next = sn;                    //   to new node
  54.     if (NULL == first)                        // item added to empty list
  55.         first = sn;
  56.     last = sn;                                // update last node pointer
  57.     sn->data = item;                        // set the data
  58.     size++;                                    // increment our size counter
  59.     return FALSE;                            // return no error
  60.     }
  61.  
  62. //    ============================================================================
  63. //    InsertNode
  64. //    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
  65. //    Inserts an element of type T before an existing SeqNode.
  66. //    ========================================|===================================
  67. template<class T>
  68. bool
  69. CSequence<T>::InsertNode(T item, SeqNode<T>* before)
  70.     {
  71.     if ((NULL == before) ||                    // before not specified, or
  72.         (before = first) )                    // insert @ first is the same
  73.         return Prepend(item);                //   thing as a prepend
  74.     SeqNode<T>*    sn = new SeqNode<T>;
  75.     if (NULL == sn)
  76.         return TRUE;                        // couldn't allocate new SeqNode
  77.     sn->prev = before->prev;                // new node -> its predecessor
  78.     if (NULL != sn->prev)
  79.         sn->prev->next = sn;                // predecessor -> new node
  80.     sn->next = before;                        // new node -> its successor
  81.     before->prev = sn;                        // successor -> new node
  82.     sn->data = item;                        // set the data
  83.     size++;                                    // increment our size counter
  84.     return FALSE;                            // return no error
  85.     }
  86.  
  87. //    ============================================================================
  88. //    RemoveNode
  89. //    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
  90. //    Excises a specified SeqNode.
  91. //    ========================================|===================================
  92. template<class T>
  93. bool
  94. CSequence<T>::RemoveNode(SeqNode<T>* node)
  95.     {
  96.     if (NULL == node)
  97.         return TRUE;
  98.  
  99.     if (first == node)                        // update first pointer if nec.
  100.         first = node->next;
  101.     if (last == node)                        // ditto for last
  102.         last = node->prev;
  103.  
  104.     if (NULL != node->prev)                    // predecessor -> successor
  105.         node->prev->next = node->next;
  106.     if (NULL != node->next)                    // successor -> predecessor
  107.         node->next->prev = node->prev;
  108.  
  109.     size--;
  110.  
  111.     delete node;                            // delete the node
  112.     return FALSE;                            // return no error
  113.     }
  114.  
  115.  
  116.